﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Logic.Application;
using LotteryVote.Logic.Web;
using LotteryVote.Utility;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote
{
    public partial class OrderListViewer : AgentBasePage
    {
        public DateTime FromDate { get; private set; }
        public DateTime ToDate { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }

        void InitParams()
        {
            int userId;
            bool descendant = false;
            if (!string.IsNullOrEmpty(Request["UserId"]) && int.TryParse(Request.QueryString["UserId"], out userId))
            {
                UserFacade userFacade = new UserFacade();
                var userParents = userFacade.GetUserFamily(userId);
                if (userParents.Find(it => it.UserId == CurrentUser.UserId) != null)
                {
                    var user = userParents.Find(it => it.UserId == userId);
                    descendant = user.Role < SystemRole.Guest;
                }
                else
                {
                    userId = CurrentUser.UserId;
                    descendant = true;
                }
            }
            else
            {
                userId = CurrentUser.UserId;
                descendant = true;
            }
            DateTime fromdate, todate;
            if (!string.IsNullOrEmpty(Request["From"]) && DateTime.TryParse(Request["From"], out fromdate))
                this.FromDate = fromdate;
            else
                this.FromDate = DateTime.Today;
            if (!string.IsNullOrEmpty(Request["To"]) && DateTime.TryParse(Request["To"], out todate))
                this.ToDate = todate;
            else
                this.ToDate = DateTime.Today;

            BindOrders(userId, descendant);

        }

        void BindOrders(int userId, bool descendant)
        {
            BetFacade betFacade = new BetFacade();
            var source = descendant ? betFacade.GetDescendantSettledOrderByUser(userId, FromDate, ToDate) :
                betFacade.GetSettledOrderByUser(userId, FromDate, ToDate);

            rpt_orderList.DataSource = source;
            rpt_orderList.DataBind();
        }

        public GamePlayWay FindGamePlayWay(object gameplayway)
        {
            int gameplaywayId = Convert.ToInt32(gameplayway);
            return LotterySystem.Current.FindGamePlayWay(gameplaywayId);
        }
        public LotteryCompany FindCompany(object company)
        {
            int companyId = Convert.ToInt32(company);
            var com = LotterySystem.Current.AllCompanyList.Find(it => it.CompanyId == companyId);
            if (com == null)
                throw new InvalidDataException("CompanyId:" + companyId);
            return com;
        }
        public string ParseToPlayWay(object gameplayway)
        {
            var gpw = FindGamePlayWay(gameplayway);
            return string.IsNullOrEmpty(gpw.PlayWay.WayName) ? string.Empty : GetGlobalResourceObject("Resource", gpw.PlayWay.WayName).ToString();
        }
    }
}